/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrameQuiz.java
 *
 * Created on 03/11/2010, 21:32:19
 */

package aprendizadosemfio.Telas;

import aprendizadosemfio.configuracoes.Configurar;
import aprendizadosemfio.configuracoes.Quizz;
import aprendizadosemfio.entidades.Pergunta;
import java.awt.Toolkit;
import java.util.ArrayList;

/**
 *
 * @author Larissa
 */
public class FrameQuiz extends javax.swing.JFrame {

    private ArrayList<Pergunta> perguntas = new Configurar().configurarQuizz();

    private int contador = 0;
    /** Creates new form FrameQuiz */
    public FrameQuiz() {
        initComponents();
        double width = Toolkit.getDefaultToolkit().getScreenSize().getWidth() / 2;
        double height = Toolkit.getDefaultToolkit().getScreenSize().getHeight() / 2;
        double widthf = getWidth() / 2;
        double heigthf = getHeight() / 2;
        int widthFinal = (int) (width - widthf);
        int heigthFinal = (int) (height - heigthf);
        setLocation(widthFinal, heigthFinal);
        atualizarPergunta();
        //conferirButton.setEnabled(false);
        respostajPanel.setVisible(false);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel2 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jRadioButton4 = new javax.swing.JRadioButton();
        jRadioButton3 = new javax.swing.JRadioButton();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        avisoJlabel = new javax.swing.JLabel();
        barra1 = new javax.swing.JToolBar();
        jButtonAnterior = new javax.swing.JButton();
        jButtonProximo = new javax.swing.JButton();
        conferirButton = new javax.swing.JButton();
        respostajPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        porcentagemAcertosjLabel = new javax.swing.JLabel();
        corretasjLabel3 = new javax.swing.JLabel();
        incorretasjLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        LabelQuestão = new javax.swing.JTextArea();

        setResizable(false);

        jPanel2.setMaximumSize(new java.awt.Dimension(700, 800));

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/quiz.png"))); // NOI18N
        jLabel2.setText("jLabel2");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 789, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel2)
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel3.setMaximumSize(new java.awt.Dimension(200, 100));
        jPanel3.setPreferredSize(new java.awt.Dimension(548, 500));

        buttonGroup1.add(jRadioButton4);
        jRadioButton4.setText("veio com um alcance menor.");
        jRadioButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton4ActionPerformed(evt);
            }
        });

        buttonGroup1.add(jRadioButton3);
        jRadioButton3.setText("foi apresentado em 2002.");
        jRadioButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton3ActionPerformed(evt);
            }
        });

        buttonGroup1.add(jRadioButton1);
        jRadioButton1.setText("foi introduzido antes que o 802.11a.");
        jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton1ActionPerformed(evt);
            }
        });

        buttonGroup1.add(jRadioButton2);
        jRadioButton2.setText("não obteve apoio por causa do alto preço do hardware e pequena capacidade.");
        jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton2ActionPerformed(evt);
            }
        });

        avisoJlabel.setForeground(new java.awt.Color(255, 51, 51));
        avisoJlabel.setText("Você ainda não respondeu essa pergunta.");

        barra1.setBorder(null);
        barra1.setFloatable(false);
        barra1.setRollover(true);

        jButtonAnterior.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/1289161796_Previous.png"))); // NOI18N
        jButtonAnterior.setText("Anterior");
        jButtonAnterior.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAnteriorActionPerformed(evt);
            }
        });
        barra1.add(jButtonAnterior);

        jButtonProximo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/1289161773_Next.png"))); // NOI18N
        jButtonProximo.setText("Próxima");
        jButtonProximo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonProximoActionPerformed(evt);
            }
        });
        barra1.add(jButtonProximo);

        conferirButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/1289161751_Check.png"))); // NOI18N
        conferirButton.setText("Conferir");
        conferirButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                conferirButtonActionPerformed(evt);
            }
        });
        barra1.add(conferirButton);

        respostajPanel.setPreferredSize(new java.awt.Dimension(236, 300));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel1.setText("Resultado:");

        porcentagemAcertosjLabel.setFont(new java.awt.Font("Tahoma", 1, 14));
        porcentagemAcertosjLabel.setText("Porcentagem de acertos: 100%");

        corretasjLabel3.setFont(new java.awt.Font("Tahoma", 1, 14));
        corretasjLabel3.setText("Respostas corretas: ");

        incorretasjLabel4.setFont(new java.awt.Font("Tahoma", 1, 14));
        incorretasjLabel4.setText("Respostas incorretas: ");

        javax.swing.GroupLayout respostajPanelLayout = new javax.swing.GroupLayout(respostajPanel);
        respostajPanel.setLayout(respostajPanelLayout);
        respostajPanelLayout.setHorizontalGroup(
            respostajPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(respostajPanelLayout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(respostajPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(incorretasjLabel4)
                    .addComponent(corretasjLabel3)
                    .addComponent(porcentagemAcertosjLabel)
                    .addComponent(jLabel1))
                .addContainerGap(45, Short.MAX_VALUE))
        );
        respostajPanelLayout.setVerticalGroup(
            respostajPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(respostajPanelLayout.createSequentialGroup()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(corretasjLabel3)
                .addGap(18, 18, 18)
                .addComponent(incorretasjLabel4)
                .addGap(18, 18, 18)
                .addComponent(porcentagemAcertosjLabel)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        LabelQuestão.setBackground(new java.awt.Color(240, 240, 240));
        LabelQuestão.setColumns(80);
        LabelQuestão.setEditable(false);
        LabelQuestão.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
        LabelQuestão.setLineWrap(true);
        LabelQuestão.setRows(2);
        LabelQuestão.setText("\n");
        LabelQuestão.setBorder(null);
        LabelQuestão.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        LabelQuestão.setEnabled(false);
        jScrollPane1.setViewportView(LabelQuestão);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 766, Short.MAX_VALUE)
                        .addGap(10, 10, 10))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jRadioButton3)
                            .addComponent(jRadioButton2)
                            .addComponent(jRadioButton4)
                            .addComponent(jRadioButton1)
                            .addComponent(avisoJlabel)
                            .addComponent(barra1, javax.swing.GroupLayout.PREFERRED_SIZE, 248, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 95, Short.MAX_VALUE)
                        .addComponent(respostajPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 276, javax.swing.GroupLayout.PREFERRED_SIZE))))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(26, 26, 26)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jRadioButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jRadioButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jRadioButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jRadioButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(avisoJlabel, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(barra1, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(respostajPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(38, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 844, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 788, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 273, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 791, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 384, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(36, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton2ActionPerformed
        perguntas.get(contador).setRespondido(1);
        atualizarPergunta();
    }//GEN-LAST:event_jRadioButton2ActionPerformed

    private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton1ActionPerformed
        perguntas.get(contador).setRespondido(0);
        atualizarPergunta();

    }//GEN-LAST:event_jRadioButton1ActionPerformed

    private void jButtonAnteriorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAnteriorActionPerformed
        // TODO add your handling code here:
        if(contador!=0)
        {   contador--;
            atualizarPergunta(); 
        }
        
    }//GEN-LAST:event_jButtonAnteriorActionPerformed

    private void jButtonProximoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonProximoActionPerformed
        // TODO add your handling code here:
        if(contador< perguntas.size()-1)
        {  
            contador++;
            atualizarPergunta();
        }
    }//GEN-LAST:event_jButtonProximoActionPerformed

    private int getResposta(){

        if(jRadioButton1.isSelected())
            return 0;
        else if(jRadioButton2.isSelected())
            return 1;

        else if(jRadioButton3.isSelected())
            return 2;

        else if(jRadioButton4.isSelected())
            return 3;
        else
            return -1;
    }

    private void marcarResposta(){

        int resposta = perguntas.get(contador).getRespondido();
        if (resposta==0)
            jRadioButton1.setSelected(true);
        else if (resposta==1)
            jRadioButton2.setSelected(true);
        else if (resposta==2)
            jRadioButton3.setSelected(true);
        else if (resposta==3)
            jRadioButton4.setSelected(true);
    }

    private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton3ActionPerformed
        perguntas.get(contador).setRespondido(2);
        atualizarPergunta();
    }//GEN-LAST:event_jRadioButton3ActionPerformed

    private void jRadioButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton4ActionPerformed
        perguntas.get(contador).setRespondido(3);
        atualizarPergunta();
    }//GEN-LAST:event_jRadioButton4ActionPerformed

    private void conferirButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_conferirButtonActionPerformed

        int quemFalta = Quizz.faltaAlguma(perguntas);
        if (quemFalta == -1) {
            respostajPanel.setVisible(true);
            int acertos = Quizz.getResultado(perguntas);
            int erros = perguntas.size()-acertos;
            int porcentagem = Quizz.getPorcentagemAcertos(perguntas.size(), acertos);
            porcentagemAcertosjLabel.setText("Porcentagem de acertos: "+porcentagem+"%");
            corretasjLabel3.setText("Respostas corretas: " +acertos);
            incorretasjLabel4.setText("Respostas incorretas: "+erros);
        } else {
            contador = quemFalta;
            atualizarPergunta();
            int temp = contador + 1;
            avisoJlabel.setText("Voce ainda não respondeu a questão " + temp + ".");
            avisoJlabel.setVisible(true);
        }

    }//GEN-LAST:event_conferirButtonActionPerformed

    private void atualizarPergunta()
    {
        Pergunta pergunta = perguntas.get(contador);
        int temp = contador+1;
        LabelQuestão.setText("Questão " +temp+ ": "+pergunta.getPergunta());
        jRadioButton1.setText(pergunta.getAlternativas().get(0));
        jRadioButton2.setText(pergunta.getAlternativas().get(1));
        jRadioButton3.setText(pergunta.getAlternativas().get(2));
        jRadioButton4.setText(pergunta.getAlternativas().get(3));
        buttonGroup1.clearSelection();
        marcarResposta();
        avisoJlabel.setVisible(false);
    }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FrameQuiz().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea LabelQuestão;
    private javax.swing.JLabel avisoJlabel;
    private javax.swing.JToolBar barra1;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton conferirButton;
    private javax.swing.JLabel corretasjLabel3;
    private javax.swing.JLabel incorretasjLabel4;
    private javax.swing.JButton jButtonAnterior;
    private javax.swing.JButton jButtonProximo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JRadioButton jRadioButton3;
    private javax.swing.JRadioButton jRadioButton4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel porcentagemAcertosjLabel;
    private javax.swing.JPanel respostajPanel;
    // End of variables declaration//GEN-END:variables

}
